/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* modifiedList(vector<int>& nums, ListNode* head) {
        unordered_set<int> table;
        ListNode* newhead = nullptr, * cur = nullptr;
        for (int i : nums) table.insert(i);
        ListNode* pcur = head;
        while (pcur) {
            if (table.find(pcur->val) == table.end()) {
                if (newhead == nullptr) {
                    newhead = new ListNode;
                    cur = pcur;
                    newhead->next = cur;
                }
                else {
                    cur->next = pcur;
                    cur = pcur;
                }
                pcur = pcur->next;
                cur->next = nullptr;
            }
            else pcur = pcur->next;
        }
        return newhead->next;
    }
};